26 Lecture

CS201

Midterm & Final Term Short Notes

Classes and Objects

Classes and objects are the fundamental building blocks of object-oriented programming (OOP). A class is a blueprint or template for creating objects, while an object is an instance of a class. Classes define the attributes and methods that obje


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a class in object-oriented programming? a. An instance of an object b. A function that returns a value c. A blueprint or template for creating objects d. A data type used for arithmetic operations

Answer: c. A blueprint or template for creating objects

  1. Which of the following is NOT an advantage of using classes and objects? a. Reusability of code b. Encapsulation of data and behavior c. Easier to understand and maintain code d. Slower program execution

Answer: d. Slower program execution

  1. Which keyword is used to create an object of a class in Python? a. create b. new c. make d. None of the above

Answer: d. None of the above (In Python, objects are created simply by calling the class as if it were a function)

  1. Which of the following is a characteristic of an object? a. Data b. Behavior c. Both A and B d. None of the above

Answer: c. Both A and B

  1. Which method is called when an object is created? a. init b. new c. create d. None of the above

Answer: b. new

  1. Which of the following is a feature of object-oriented programming? a. Inheritance b. Encapsulation c. Polymorphism d. All of the above

Answer: d. All of the above

  1. What is inheritance in object-oriented programming? a. The process of creating a new object from an existing object b. The ability to create a new class from an existing class c. The process of adding new methods to a class d. The ability to call methods from another class

Answer: b. The ability to create a new class from an existing class

  1. What is encapsulation in object-oriented programming? a. The ability to hide the internal workings of an object b. The process of creating a new object from an existing object c. The ability to call methods from another class d. The process of adding new methods to a class

Answer: a. The ability to hide the internal workings of an object

  1. What is polymorphism in object-oriented programming? a. The ability to create a new class from an existing class b. The process of adding new methods to a class c. The ability of objects of different classes to be treated as if they were of the same class d. The ability to call methods from another class

Answer: c. The ability of objects of different classes to be treated as if they were of the same class

  1. What is the difference between a class and an object? a. A class is a blueprint for creating objects, while an object is an instance of a class. b. A class is an instance of an object, while an object is a blueprint for creating classes. c. A class and an object are the same thing. d. None of the above.

Answer: a. A class is a blueprint for creating objects, while an object is an instance of a class.



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a constructor in a class? Answer: A constructor is a special method in a class that is called when an object of that class is created. It is used to initialize the attributes of the object.

  2. What is inheritance in object-oriented programming? Answer: Inheritance is the process of creating a new class from an existing class. The new class inherits the attributes and methods of the existing class, and can add its own attributes and methods as well.

  3. What is encapsulation in object-oriented programming? Answer: Encapsulation is the practice of hiding the internal workings of an object from the outside world, and exposing only a public interface for interacting with the object.

  4. What is polymorphism in object-oriented programming? Answer: Polymorphism is the ability of objects of different classes to be treated as if they were of the same class. This allows for more flexible and dynamic code.

  5. What is a method in a class? Answer: A method is a function defined within a class, which can access and manipulate the data attributes of an object of that class.

  6. What is an instance variable in a class? Answer: An instance variable is a variable defined within a class, and is specific to each object created from that class.

  7. What is a static variable in a class? Answer: A static variable is a variable defined within a class, but is shared by all objects of that class.

  8. What is the difference between a class and an object? Answer: A class is a blueprint or template for creating objects, while an object is an instance of a class.

  9. What is the difference between a method and a function? Answer: A method is a function defined within a class, and is associated with objects of that class. A function, on the other hand, is not associated with any particular class or object.

  10. What is the relationship between a superclass and a subclass? Answer: A subclass is a new class created from an existing class (the superclass), and inherits the attributes and methods of the superclass. The subclass can add its own attributes and methods as well.

In object-oriented programming, classes and objects are key concepts. A class is a blueprint or template for creating objects, while an object is an instance of a class. A class defines the attributes and methods that an object will have. The attributes are the data variables that describe the state of an object, while the methods are the functions that define the behavior of an object. One of the key advantages of using classes and objects is the ability to encapsulate data and functionality. This means that the internal workings of an object are hidden from the outside world, and only a public interface is exposed for interacting with the object. This makes code more modular and easier to maintain, as changes can be made to the internal workings of an object without affecting the rest of the code. Inheritance is another important concept in object-oriented programming. It allows for the creation of a new class from an existing class, with the new class inheriting the attributes and methods of the existing class. This promotes code reuse and helps to create a hierarchy of related classes. Polymorphism is the ability of objects of different classes to be treated as if they were of the same class. This allows for more flexible and dynamic code, as different objects can be used interchangeably in the same context. To create an object from a class, a constructor method is used. This method is called when an object is created, and is used to initialize the attributes of the object. Overall, the use of classes and objects in object-oriented programming provides a powerful and flexible way to organize and structure code, promote code reuse, and create complex systems that are easy to maintain and extend over time.